package com.ruoyi.custservice.controller;

import com.ruoyi.bizsys.domain.ProductInfo;
import com.ruoyi.bizsys.service.IProductInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.RspEnmu;
import com.ruoyi.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/business/ProductInfo")
public class BProductInfoController extends BaseController {

    @Autowired
    private IProductInfoService productInfoService;

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('business:ProductInfo:submit')")
    @Log(title = "产品信息", businessType = BusinessType.SUBMIT)
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody ProductInfo productInfo) {
        ProductInfo productInfoQ = new ProductInfo();
        productInfoQ.setProductCode(productInfo.getProductCode());
        List<ProductInfo> productInfos = productInfoService.selectProductInfoList(productInfoQ);
        if (productInfos.size() > 0){
            throw new BusinessException(RspEnmu.PRODUCT_CODE_EXISTS);
        }
        productInfoQ.setProductCode(null);
        productInfoQ.setProductType(productInfo.getProductType());
        productInfoQ.setPeriodNum(productInfo.getPeriodNum());
        productInfos = productInfoService.selectProductInfoList(productInfoQ);
        if (productInfos.size() > 0){
            throw new BusinessException(RspEnmu.DATA_ALREADY_EXISTS);
        }
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }
}
